#include "esapi.h"

u32 sys_ticks = 0;

void AddTick(void)
{
    sys_ticks++;
}

u32 GetTickCount(void)
{
    return sys_ticks;
}

void ClearTicks(void)
{
    sys_ticks = 0;
}

void delay(u32 t)
{
    u32 begin = GetTickCount();
    while (GetTickCount() < (begin + t))
	;
}


void NVIC_Control(u8 vector, u8 priority)
{
    if (priority == 0)	return; //不能设置为优先级0    
    if (priority == 2)	priority = 0;   

    //优先级2:00B
    if (vector < 4)
    {
	ITC->ISPR1 &= ~(3 << vector * 2);
	//清除原来的设置
	ITC->ISPR1 |= priority << vector * 2;
	//设置优先级
    }
    else if (vector < 8)
    {
	ITC->ISPR2 &= ~(3 << (vector - 4) * 2);
	//清除原来的设置
	ITC->ISPR2 |= priority << (vector - 4) * 2;
	//设置优先级
    }
    else if (vector < 12)
    {
	ITC->ISPR3 &= ~(3 << (vector - 8) * 2);
	//清除原来的设置
	ITC->ISPR3 |= priority << (vector - 8) * 2;
	//设置优先级
    }
    else if (vector < 16)
    {
	ITC->ISPR4 &= ~(3 << (vector - 12) * 2);
	//清除原来的设置
	ITC->ISPR4 |= priority << (vector - 12) * 2; //设置优先级
    }
    else if (vector < 20)
    {
	ITC->ISPR5 &= ~(3 << (vector - 16) * 2);
	//清除原来的设置
	ITC->ISPR5 |= priority << (vector - 16) * 2; //设置优先级
    }
    else if (vector < 24)
    {
	ITC->ISPR6 &= ~(3 << (vector - 20) * 2);
	//清除原来的设置
	ITC->ISPR6 |= priority << (vector - 20) * 2; //设置优先级
    }
    else if (vector < 28)
    {
	ITC->ISPR7 &= ~(3 << (vector - 24) * 2);
	//清除原来的设置
	ITC->ISPR7 |= priority << (vector - 24) * 2; //设置优先级
    }
    else if (vector < 32)
    {
	ITC->ISPR8 &= ~(3 << (vector - 28) * 2);
	//清除原来的设置
	ITC->ISPR8 |= priority << (vector - 28) * 2; //设置优先级
    }
}